package com.hf.eagle.web.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.DynaBean;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hf.eagle.domain.Accessory;
import com.hf.eagle.domain.NewsInfo;
import com.hf.eagle.domain.PromotionDesc;
import com.hf.eagle.domain.SysDictionary;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.Pager;
import com.hf.eagle.web.base.SysConstant;

/**
 * 促销动态
 * @author xiaokeheng
 *
 */
public class SalesPromotionDynamicsAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.view(mapping, form, request, response);
	}

	/**
	 * 显示促销动态
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String id = (String) dynaBean.get("id");
		String mod_id = (String) dynaBean.get("mod_id");

		NewsInfo newsInfo = new NewsInfo();
		newsInfo.setId(Long.valueOf(id));
		newsInfo = super.getFacade().getNewsInfoService().getNewsInfo(newsInfo);
		if (null != newsInfo) {
			newsInfo.setView_count(newsInfo.getView_count()+1);
			super.getFacade().getNewsInfoService().modifyNewsInfo(newsInfo);
			Accessory acce = new Accessory();
			acce.setFile_level(SysConstant.MAIN_IMG);
			acce.getMap().put("mod_id", mod_id);
			acce.getMap().put("record_id", newsInfo.getId());
			acce = super.getFacade().getAccessoryService().getAccessory(acce);

			newsInfo.setAccessory(acce);

			PromotionDesc pd = new PromotionDesc();
			pd.setNews_id(Long.valueOf(id));
			List<PromotionDesc> pdList = super.getFacade().getPromotionDescService().getPromotionDescList(pd);
			newsInfo.setPromotionDescList(pdList);
		}
		request.setAttribute("newsInfo", newsInfo);
		return mapping.findForward("view");
	}
	
	/**
	 * 显示促销动态列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean lazyForm = (DynaBean) form;
		String mod_id = (String) lazyForm.get("mod_id");
		NewsInfo newsInfo=new NewsInfo();
		newsInfo.setMod_id(Long.parseLong(mod_id));
		newsInfo.setIs_del(0);
		super.copyProperties(newsInfo, form);
		Pager pager = (Pager) lazyForm.get("pager");
		Long recordCount = super.getFacade().getNewsInfoService().getNewsInfoCount(newsInfo);
		pager.init(recordCount, pager.getPageSize(), pager.getRequestPage());
		newsInfo.getRow().setFirst(pager.getFirstRow());
		newsInfo.getRow().setCount(pager.getLastRow());

		List<NewsInfo> salesPromotionDynamics = super.getFacade().getNewsInfoService().getNewsInfoPaginatedList(newsInfo);
		
        request.setAttribute("mod_id", mod_id);
        request.setAttribute("salesPromotionDynamics", salesPromotionDynamics);
		return mapping.findForward("list");
	}

}